
// 本地更新需要更改此版本
const newVersion = "1.2.300"

function Version (storageKey) {
  this.storage = storages.create(storageKey);
  this.version = this.storage.get('version')
  log('this.version', this.version)


  if (this.version < newVersion) {
    this.version = newVersion
    this.set(newVersion)
  }
  if (!this.version) {
    this.version = newVersion
    this.storage.put('version', this.version)
  }

}

Version.prototype.set = function (version) {
  log('当前版本设置为：', version)
  this.storage.put('version', version)
  log('当前版本为:', this.storage.get('version'))
  this.version = version
}

Version.prototype.get = function () {
  return this.version
}

Version.prototype.isNeedToUpdateFile = function (newVersion) {
  log('newVersion', newVersion)
  log('localVersion', this.version)
  if (this.version < newVersion) {
    // 需要更新
    return true
  } else {
    return false
  }
}


Version.prototype.formatDate = function (value, format) {
  value = value || Date.now()
  format = format || "Y-M-D h:m:s"
  const formatNumber = n => `0${n}`.slice(-2);
  const date = new Date(value);
  const formatList = ["Y", "M", "D", "h", "m", "s"];
  const resultList = [];
  resultList.push(date.getFullYear().toString());
  resultList.push(formatNumber(date.getMonth() + 1));
  resultList.push(formatNumber(date.getDate()));
  resultList.push(formatNumber(date.getHours()));
  resultList.push(formatNumber(date.getMinutes()));
  resultList.push(formatNumber(date.getSeconds()));
  for (let i = 0; i < resultList.length; i++) {
    format = format.replace(formatList[i], resultList[i]);
  }
  return format;
}

Version.prototype.baseApi = function (fileName) {
  const config = {
    url: "https://dav.jianguoyun.com/dav/",
    path: '金手指/',
    file: 'version.json',
    user: '1526859282@qq.com',
    key: 'a3i8urnwqk2ib496'
  }

  return http.get(config.url + config.path + fileName,
    {
      headers: {
        "Authorization": "Basic " + java.lang.String(android.util.Base64.encode(java.lang.String(config.user + ':' + config.key).getBytes(), 2)),
        // "Content-Type": "text/plain;charset=UTF-8"
      }
    })
}


module.exports = Version